package com.xwltz.rbac.auth;

import cn.dev33.satoken.interceptor.SaRouteInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @description TokenConfigure
 * @information on version 1.0 be created by @xwltz at 2022/6/17 16:58
 */

@Configuration
public class TokenConfigure implements WebMvcConfigurer {
	// 注册 Token 的拦截器
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// 注册路由拦截器，自定义认证规则
		registry.addInterceptor(new SaRouteInterceptor((req, res, handler)->{

			// 登录认证 -- 拦截所有路由，并排除/member/login 用于开放登录
			SaRouter.match("/**", "/member/signIn", r -> StpUtil.checkLogin());

			// 根据路由划分模块，不同模块不同鉴权
			SaRouter.match("/album/**", r -> StpUtil.checkPermission("album"));
		})).addPathPatterns("/**");
	}
}
